package com.zds.hj.poseidon.biz.dispatcher.credit.demo;

import com.zds.boot.common.context.ZDSContext;
import com.zds.boot.dispatch.annotation.DispatcherHandler;
import com.zds.boot.dispatch.comm.enums.ModuleNodeTypeEnum;
import com.zds.boot.dispatch.dto.BaseReqModel;
import com.zds.boot.dispatch.dto.BaseResultModel;
import com.zds.boot.dispatch.dto.ModuleBaseInfo;
import com.zds.component.files.facade.dto.FileAuthDto;
import com.zds.hj.icmc.facade.dto.capitallnfo.CapitalInfoDto;
import com.zds.hj.poseidon.biz.common.FileServiceInterface;
import com.zds.hj.poseidon.biz.common.IcmcServiceInterface;
import com.zds.hj.poseidon.biz.dispatcher.credit.CreditCheckAbsHandler;
import com.zds.hj.poseidon.facade.dto.hpp.credit.CreditInfoEntityDto;
import com.zds.hj.product.facade.constant.ApplicationContainerConstant;
import com.zds.user.facade.pbac.enums.UserTypeEnum;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ZY
 */
@DispatcherHandler(value = "CreditFilesForThirdSideHandler", desc = "三方赋予文件权限", applicationContainer = ApplicationContainerConstant.FRONT_END, nodeType = ModuleNodeTypeEnum.BUSINESS)
@Slf4j
public class CreditFilesForThirdSideHandler extends CreditCheckAbsHandler {


    @Resource
    private FileServiceInterface fileServiceInterface;

    @Resource
    private IcmcServiceInterface icmcServiceInterface;

    @Override
    public void creditDoExecute(BaseReqModel params, BaseResultModel resultModel, CreditInfoEntityDto reqData, CreditInfoEntityDto dbData) {

        log.info("三方赋予文件权限，订单号：{}", dbData.getApplyNo());
        //64632为测试数据资金方编号dbData.getCapitalSideNo()
        CapitalInfoDto capitalInfoByCapitalNo = icmcServiceInterface.getCapitalByNo(dbData.getCapitalSideNo());
        FileAuthDto fileAuthDto = new FileAuthDto();
        fileAuthDto.setObsId(dbData.getProductId());

        dbData.setEntCreditCode("46456456456");
        dbData.setProtocolFile(dbData.getBatchNo());

        List<String> obsIds = new ArrayList<>();
        //身份证
        obsIds.addAll(Arrays.asList(dbData.getCardFrontObsId().split("[,;]")));
        obsIds.addAll(Arrays.asList(dbData.getCardBackObsId().split("[,;]")));
        //营业执照
        obsIds.addAll(Arrays.asList(dbData.getEntCreditCode().split("[,;]")));
        //协议文件
        obsIds.addAll(Arrays.asList(dbData.getProtocolFile().split("[,;]")));

        FileAuthDto fileAuthDto1 = FileAuthDto.builder().buildUserId(ZDSContext.get().getSeUserInfo().getId()).buildOrgId(capitalInfoByCapitalNo.getOrgId()).buildOrgType(UserTypeEnum.PLATFORM.name()).build();
        for (String obsId:obsIds) {
            fileAuthDto1.setObsId(Long.parseLong(obsId));
            fileServiceInterface.addFilesAuth(fileAuthDto);
        }

        log.info("三方赋予文件权限,{},{}",capitalInfoByCapitalNo.getCapitalSideNo(),capitalInfoByCapitalNo.getCapitalName());
    }

    /**
     * 设置为节点(可逆，同步，自动节点)
     * @return ModuleBaseInfo
     */
    @Override
    public ModuleBaseInfo moduleBaseInfo() {
        ModuleBaseInfo moduleBaseInfo = super.moduleBaseInfo();
        moduleBaseInfo.setAuto(true);
        moduleBaseInfo.setAsyn(false);
        moduleBaseInfo.setReverse(true);
        return moduleBaseInfo;
    }
}
